/*
 * @Author: 贾博林 11457230+jabolin@user.noreply.gitee.com
 * @Date: 2023-03-29 10:48:24
 * @LastEditors: 贾博林 11457230+jabolin@user.noreply.gitee.com
 * @LastEditTime: 2023-03-31 10:09:09
 * @FilePath: \zs-grouting2\naive-ui-admin\src\modules\system\department\department.service.ts
 * @Description: 登录日志
 */
import { useCurd, useForm, useTable } from '@/hooks/useCurd'
import { PaginationDto } from '@/modules/common/common.dto'
import { Injectable } from 'butterfly-decorators'
import { Application, ApplicationController } from './systemlogs.controller'

export const tableState = useTable<Application>()
export const formState = useForm<Application>()

export const init = () => {
  tableState.columns = [
    { title: '登录时间', key: 'departmentName', sorter: (row1, row2) => row1.age - row2.age },
    { title: '登录ip', key: 'departmentDesc' },
    {
      title: '用户账号',
      key: 'departmentLogo',
      sorter: {
        compare: (a, b) => a.chinese - b.chinese,
        multiple: 3
      }
    },
    {
      title: '登出时间',
      key: 'apiBaseUrl',
      sorter: {
        compare: (a, b) => a.math - b.math,
        multiple: 2
      }
    }
  ]
  formState.template = [
    {
      type: 'upload',
      label: '头像/照片:',
      span: 24,
      offset: 0,
      uploadProps: {
        name: 'avatarfile'
      }
    },
    {
      type: 'select',
      label: '用户名:',
      keyPath: 'applicationName',
      selectProps: {
        options: [],
        placeholder: '请输入用户名'
      }
    },
    {
      type: 'text',
      label: '密码:',
      keyPath: 'applicationDir',
      textProps: { placeholder: '请输入密码' }
    },
    {
      type: 'text',
      label: '姓名:',
      keyPath: 'applicationDir',
      textProps: { placeholder: '请输入姓名' }
    },
    {
      type: 'text',
      label: '公司:',
      keyPath: 'applicationDir',
      textProps: { placeholder: '请输入公司' }
    },
    {
      type: 'text',
      label: '角色:',
      keyPath: 'applicationDir',
      textProps: { placeholder: '请选择角色' }
    },
    {
      type: 'text',
      label: '年龄:',
      keyPath: 'applicationDir',
      textProps: { placeholder: '请输入年龄' }
    },
    {
      type: 'text',
      label: '手机号:',
      keyPath: 'applicationDir',
      textProps: { placeholder: '请输入手机号' }
    },
    {
      type: 'upload',
      label: '电子签名:',
      span: 24,
      offset: 0,
      uploadProps: {
        name: 'avatarfile'
      }
    }
  ]
}

@Injectable({
  controllers: [ApplicationController]
})
export class ApplicationService {
  static applicationController: ApplicationController

  // static async getDepartmentList() {
  //   console.log(formState.template![0])
  //   formState.template![0].selectProps!.options = [
  //     { label: '开发部', value: 1 },
  //     { label: '管理部', value: 2 },
  //     { label: '检测部', value: 3 },
  //     { label: '业主部', value: 4 }
  //   ]
  // }

  static async getApplicationList({ page, size }: PaginationDto) {
    const res = await this.applicationController.findListReq({ page, size })!
    if (res.code === 200) {
      res.data.list.forEach((item) => {
        item.applicationLogo = item.applicationLogo
          ? import.meta.env.BUTTERFLY_BASE_API_URL + item.applicationLogo
          : ''
      })
      return { list: res.data.list, total: res.data.pageInfo.total }
    }
    return {
      list: [
        {
          departmentName: ['2:00:10'],
          departmentDesc: ['169.131.121'],
          departmentLogo: ['张三'],
          apiBaseUrl: ['5:16:31']
        },
        {
          departmentName: ['3:00:21'],
          departmentDesc: ['169.141.131'],
          departmentLogo: ['李四'],
          apiBaseUrl: ['6:25:26']
        }
      ],
      total: 0
    }
  }

  static async createOne(newData: Application) {
    const res = await this.applicationController.addOneReq(newData)!
    if (res.code == 200) {
      return { success: true, msg: '操作成功' }
    } else {
      return { success: false, msg: res.message }
    }
  }

  static async updateOne(newData: Application) {
    const res = await this.applicationController.updateOneReq(newData)!
    if (res.code === 200) {
      return { success: true, msg: '操作成功' }
    } else {
      return { success: false, msg: res.message }
    }
  }

  static async deleteByIds(ids: any[]) {
    const res = await this.applicationController.deleteByIdsReq(ids)!
    if (res.code === 200) {
      return { success: true, msg: '删除成功' }
    } else {
      return { success: false, msg: res.message }
    }
  }
}

export const { r, c, u, d } = useCurd({
  r(page, size) {
    return ApplicationService.getApplicationList({ page, size })
  },
  c(newData) {
    return ApplicationService.createOne(newData)
  },
  u(newData) {
    return ApplicationService.updateOne(newData)
  },
  d(keys) {
    return ApplicationService.deleteByIds(keys)
  }
})
